<?php
include('../../../../inc/application_settings.php');

$email = $_COOKIE['EMAIL'];
$target = (isset($_REQUEST['T'])) ? $_REQUEST['T'] : null;
$galeryId = (isset($_REQUEST['GALLERY_ID'])) ? $_REQUEST['GALLERY_ID'] : null;
$imageId = (isset($_REQUEST['IMAGE_ID'])) ? $_REQUEST['IMAGE_ID'] : null;
$password = Cryptic::decrypt($_COOKIE['PASSWORD'], $email);
$auth = new Authentication($email, $password);

if ($auth->checkAuthentication()){
	switch ($target) {
		case 'NEW':
			$output = loadForm();
			break;
		case 'EDIT':
			$output = loadForm($galeryId);
			break;
		case 'IMAGE':
			$output = loadImageForm($imageId);
			break;
	}
} else 
	$output = 'Not authenticated!';
echo $output;


/**
 * generates the output for german and english form to create a gallery entry
 * encloses the both forms with list for tabs
 * create tab functionality on javascript side
 * @param		int		$galleryId
 */
function loadForm($galleryId = null){
	$output = '<div id="galleryTabs">';
	$output .= loadTabList();
	$output .= loadBasicForm('de', $galleryId);
	$output .= loadBasicForm('en', $galleryId);
	$output .= '</div>';
	
	if ($galleryId != null)
		$output .= loadUploadForm($galleryId);
	
	$output .= '<div id="gallery_image_preview">' . loadImages($galleryId) . '</div>';
	return $output;
}


/**
 * loads the form for uploading images
 */
function loadUploadForm($galleryId){
	$output = '<form id="uploadForm" action="' . GALLERY_FILE_URL . '?T=UPLOAD&GALLERY_ID=' . $galleryId . '" enctype="multipart/form-data" method="post">';
	$output .= 		'<input id="uploadFormButton" type="button" onclick="showUploadSection();" value="Upload Images" />';
	$output .= 		'<div id="uploadFields" style="display:none">';
	$output .=			'<br />';
	$output .=			'<span class="mandatory">File:</span> ';
	$output .=			'<input name="fileName" id="fileName" type="file" accept="image/*" />';
	$output .=			'<input id="uploadButton" type="button" onclick="initUpload();" value="Upload" />';
	$output .=			'<span id="uploadProgress" style="font-weight:bold">&nbsp;</span>';
	$output .=		'</div>';
	$output .= '</form>';
	return $output;
}


/**
 * loads the div for tablist
 * @return		string		$output
 */
function loadTabList(){
	$output = <<<TABLIST
	<div class="toolbar tabbar" style="width: 100%">
		<ul class="tabs"> 
		    <li class="first"><a class="selected" href="#gallery_de">Deutsch</a></li> 
		    <li><a href="#gallery_en">Englisch</a></li> 
	  	</ul>
  	</div>
TABLIST;
	return $output; 
}



/**
 * generates the form for english and german
 * @param 		string		$language
 * @param 		int			$galleryId
 * @return		string		$output
 */
function loadBasicForm($language, $galleryId = null){
	if ($galleryId != null)
		$gallery = getGalleryData($galleryId, $language);
		
	$output = '<div id="gallery_' . $language . '" style="margin-top: 10px">';
	$output .= '<table border="0">';
	$output .= 		'<tr>';
	$output .= 			'<td><span class="mandatory">Name:</span></td>';
	$output .= 			'<td><input type="text" id="gallery_name_' . $language . '" style="width: 150px" value="' . (($gallery != null) ? $gallery['name_' . $language] : '') . '"/></td>';
	
	if ($galleryId != null){
		$checkedStatus = ($gallery['active']) ? 'checked="checked"' : '';
		$output .= 		'<tr>';
		$output .= 			'<td><span>Aktiv:</span></td>';
		$output .= 			'<td><input id="gallery_active_' . $language . '" type="checkbox" ' . $checkedStatus . '/></td>';
		$output .= 		'</tr>';
	}
	$output .= '</table>';

	$output .= '</div>';
	return $output;
}


function getGalleryData($galleryId, $language){
	$galleryArr = null;
	
	$sql = 'select name_' . $language . ', active from ' . DB_BIO . '.gallery_view where gallery_id = ?';
	$params = array($galleryId);
	
	$sqlObj = Sql_Object::getInstance();
	$sqlObj->connect();
	$sqlObj->execute($sql, $params);
	
	if (($row = $sqlObj->fetch()) != null){
		$galleryArr = array();
		foreach($row as $gallery => $attribute)
			$galleryArr[$gallery] = $attribute;
	}
	
	$sqlObj->disconnect();
	return $galleryArr;
}


function loadImages($galleryId){
	$output = '';
	$sql = 'select file_id, image_id from ' . DB_BIO . '.gallery_image where gallery_id = ?';
	$params = array($galleryId);
	
	$sqlObj = Sql_Object::getInstance();
	$sqlObj->connect();
	$sqlObj->execute($sql, $params);
	
	while (($row = $sqlObj->fetch()) != null)
		$output .= '<div class="imagePreview"><img style="display: none" src="' . GALLERY_THUMBNAIL_URL . '?SHOW_ACTIVE=1&FILE_ID=' . $row['file_id'] . '" onclick="editImage(' . $row['image_id'] . ');" /></div>';
	
	$sqlObj->disconnect();
	return $output;
}


function loadImageForm($imageId){
	$output = '';
	if (is_numeric($imageId)){
		$image = getImageData($imageId);
		
		$output .= '<form id="imageForm" method="post" action="' . GALLERY_FILE_URL . '?T=DOWNLOAD&FILE_ID=' . $image['file_id'] . '">';
		$output .= '<div id="imageTabs">';
		$output .= loadImageTabList();
		$output .= loadBasicImageForm('de', $imageId, $image);
		$output .= loadBasicImageForm('en', $imageId, $image);
		$output .= '<input type="submit" id="downloadImage" onclick="downloadImage(' . $image['file_id'] . ');" value="Download" />';
		$output .= '<input type="button" id="deleteImage" onclick="deleteImg(' . $imageId . ',' . $image['file_id'] . ',' . $image['gallery_id'] . ');" value="L&ouml;schen" />';
		$output .= '<input type="button" id="scaleImage" onclick="scaleImg(true);" value="+" />';
		$output .= '<input type="button" id="scaleImage" onclick="scaleImg(false);" value="-" />';
		$output .= '</div><br />';
		$output .= '<div id="imagePreview">';
		$output .= '<img src="' . GALLERY_THUMBNAIL_URL . '?WIDTH=300&HEIGHT=300&SHOW_ACTIVE=0&FILE_ID=' . $image['file_id'] . '" />';
		$output .= '</div>';
		$output .= '</form>';
	}
	return $output;
}


/**
 * loads the div for tablist
 * @return		string		$output
 */
function loadImageTabList(){
	$output = <<<TABLIST
	<div class="toolbar tabbar" style="width: 100%">
		<ul class="tabs">
		    <li class="first"><a class="selected" href="#image_de">Deutsch</a></li>
		    <li><a href="#image_en">Englisch</a></li>
	  	</ul>
  	</div>
TABLIST;
	return $output;
}



/**
 * generates the form for english and german
 * @param 		string		$language
 * @param 		int			$imageId
 * @param		mixed		$image
 * @return		string		$output
 */
function loadBasicImageForm($language, $imageId, $image){
	$output = '<div id="image_' . $language . '" style="margin-top: 10px">';
	$output .= '<table border="0">';
	$output .= 		'<tr>';
	$output .= 			'<td><span class="mandatory">Name:</span></td>';
	$output .= 			'<td><input type="text" id="image_name_' . $language . '" style="width: 150px" value="' . (($image != null) ? $image['name_' . $language] : '') . '"/></td>';
		
	$checkedStatus = ($image['active']) ? 'checked="checked"' : '';
	$output .= 		'<tr>';
	$output .= 			'<td><span>Aktiv:</span></td>';
	$output .= 			'<td><input id="image_active_' . $language . '" type="checkbox" ' . $checkedStatus . '/></td>';
	$output .= 		'</tr>';
	
	$output .= 		'<tr>';
	$output .=			'<td><span class="mandatory">Anzeigebreite (px):</span></td>';
	$output .=			'<td><input type="text" id="image_width_' . $language . '" style="width: 150px" value="' . $image['width'] . '"/>';
	$output .=		'</tr>';
	
	$output .= 		'<tr>';
	$output .=			'<td><span class="mandatory">Anzeigeh&ouml;he (px):</span></td>';
	$output .=			'<td><input type="text" id="image_height_' . $language . '" style="width: 150px"  value="' . $image['height'] . '"/>';
	$output .=		'</tr>';
	
	$output .= '</table>';

	$output .= '</div>';
	return $output;
}


/**
 * returns an array of all image attributes
 * @param 		int		 $imageId
 */
function getImageData($imageId){
	$imageArr = null;
	
	$sql = 'select * from ' . DB_BIO . '.gallery_image_view where image_id = ?';
	$params = array($imageId);
	
	$sqlObj = Sql_Object::getInstance();
	$sqlObj->connect();
	$sqlObj->execute($sql, $params);
	
	if (($row = $sqlObj->fetch()) != null){
		$imageArr = array();
		foreach($row as $image => $attribute)
			$imageArr[$image] = $attribute;
	}
	
	$sqlObj->disconnect();
	return $imageArr;
}

?>